package com.nuaa.dao.impl;

import java.text.MessageFormat;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.nuaa.dao.base.impl.BaseDaoImpl;
import com.nuaa.dao.inter.OrderDao;
import com.nuaa.domain.OrderEntity;

@Repository("orderDao")
public class OrderDaoImpl extends BaseDaoImpl<OrderEntity, Integer> implements OrderDao{

	
	@Override
	public long count(String customNo){
		String params = "id,order_no,customer_no,create_tm,business_type,state,laborFee,postage";
		StringBuffer sqlSb = new StringBuffer(MessageFormat.format("select * from (select {0} from backup_order union "
				+"select {0} from change_order union "
				+"select {0} from local_dis_order union "
				+"select {0} from return_order union "
				+"select {0} from transfer_order  ) as s ", params));
		if (customNo!=null &&!customNo.equals("")) {
			sqlSb.append(" where s.customer_no ='");
			sqlSb.append(customNo);
			sqlSb.append("'");
		}
		return super.count(sqlSb.toString());
	}
	
	@Override
	public List<OrderEntity> selectAll(String customNo,int index,int last){
		String params = "id,order_no,customer_no,create_tm,business_type,state,laborFee,postage";
		StringBuffer sqlSb = new StringBuffer(MessageFormat.format("select * from (select {0} from backup_order union "
				+"select {0} from change_order union "
				+"select {0} from local_dis_order union "
				+"select {0} from return_order union "
				+"select {0} from transfer_order ) as s ", params));
		if (customNo!=null && !"".equals(customNo)) {
			sqlSb.append(" where s.customer_no ='");
			sqlSb.append(customNo);
			sqlSb.append("'");
		}
		sqlSb.append(" order by create_tm desc");
		//System.out.println(sqlSb.toString());
		List<OrderEntity> list = this.search(OrderEntity.class, sqlSb.toString(),index,last);
		return list;
	}
}
